package StudyCode.summary.regularexpressionsmatch;

import java.util.LinkedList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class SplittingStringsWithPattern {
	public static void main(String[] argv) {
		String input = "test test zoo";
		Pattern space = Pattern.compile(" ");
		Pattern d = Pattern.compile("d");
		Pattern o = Pattern.compile("o");
		Pattern[] patterns = {space, d, o};
		int limits[] = {1, 2, 5, -2, 0};

		patterns = collectPatterns(new String[]{"zoo"});
		generateTable(input, patterns, limits);
	}

	private static void generateTable(String input, Pattern[] patterns,
	                                  int[] limits) {
		System.out.println(input);
		for (Pattern pattern : patterns) {
			System.out.println(pattern.pattern());
		}
		for (int limit : limits) {
			System.out.println(limit);
			for (Pattern pattern : patterns) {
				String[] tokens = pattern.split(input, limit);
				for (String token : tokens) {
					System.out.print("value:" + token);
				}
			}
		}
	}

	private static Pattern[] collectPatterns(String[] argv) {
		List<Pattern> list = new LinkedList<>();
		for (int i = 1; i < argv.length; i++) {
			list.add(Pattern.compile(argv[i]));
		}
		Pattern[] patterns = new Pattern[list.size()];
		list.toArray(patterns);
		return (patterns);
	}
}
